'use strict';

const db = uniCloud.database();
const collection = db.collection('medicine');

exports.main = async (event, context) => {
	// 查询是否为重复添加
	const res = await collection.where({ name: event.name }).get();

	if (res.data.length > 0) {
		return {
			code: 400,
			msg: '该药品已存在'
		};
	}

	// 添加药品
	const result = await collection.add(event);


	//返回数据给客户端
	return {
		code: 0,
		msg: '添加成功',
		data: result
	};
};